package com.demo0215.controller;

import com.demo0215.entity.User;
import com.demo0215.entity.UserVo;
import com.demo0215.feign.UserFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

@Controller
@RefreshScope
@RequestMapping("/user")
public class UserConsumerController {
    @Autowired
    private UserFeign userFeign;

    @GetMapping("/findAll")
    @ResponseBody
    public UserVo findAll(@RequestParam("page") int page, @RequestParam("limit") int limit){
        int index = (page-1)*limit;
        UserVo userVo = userFeign.findAll(index, limit);
        return userVo;
    }

    @GetMapping("deleteById/{id}")
    public String deleteById(@PathVariable("id") long id){
        userFeign.deleteById(id);
        return "redirect:/redirect/user_manage";
    }


    @PostMapping("/save")
    public String save(User user){
        userFeign.save(user);
        return "redirect:/user/redirect/user_manage";
    }

    @PostMapping("/update")
    public String update(User user){
        userFeign.update(user);
        return "redirect:/user/redirect/user_manage";
    }
}
